#!/bin/sh

. ./conf/global
. ./conf/openldap

# -------------------------------------------------------------
# Use 'dialog' for configuration.
# -------------------------------------------------------------
DIALOG='dialog'
DIALOG_BACKTITLE='OpenBSD Mail Server Installation Wizard (For OpenBSD 4.2 -release)'

# Welcome message.
${DIALOG} --backtitle "${DIALOG_BACKTITLE}" \
    --title "Thanks for use" \
    --msgbox "\
Thanks for your use of this script for mail server.
Feedback, bug report, communication are all welcome.

Contact me if you need help about this script or OpenBSD:

    * Author:       Bibby
    * Mail/GTalk:   michaelbibby (at) gmail.com
    * MSN:          michaelbibby (at) hotmail.com

Ctrl-C will abort this wizard.

" \
    20 78

# Main features.
${DIALOG} --backtitle "${DIALOG_BACKTITLE}" \
    --title "Main Features" \
    --msgbox "\
The mail server contains the following main features:

    * SMTP (Use Postfix, with LDAP support)
    * POP3/IMAP (Use Courier POP3/IMAP)
    * Virtual Domains/Users (Use OpenLDAP, with phpLDAPadmin)
    * Web Mail (Use SquirrelMail, i18n support, with useful plugins)
        * Calendar, Global LDAP Address Book, Check Quota, Select Language
    * Anti-Spam (Use spamd(8) in base system, SpamAssassin)
    * Anti-Virus (Use ClamAV)

" \
    20 78

# PKG_PATH
${DIALOG} --backtitle "${DIALOG_BACKTITLE}" \
    --title "PKG_PATH variable" \
    --inputbox "\
We will use PKG_PATH to install binary packages.
Most of packages can use binary package directly, it's recommended.

Note:

    * PKG_PATH variable MUST end in a slash (/).
    * OpenBSD official ftp site is *NOT* recommended.

EXAMPLES:

    * ftp://ftp.tw.openbsd.org/pub/OpenBSD/$(uname -r)/packages/$(machine -a)/

" \
    20 78 \
    "${PKG_PATH}" \
    2>/tmp/pkg_path

PKG_PATH="$(cat /tmp/pkg_path)"
echo "PKG_PATH='${PKG_PATH}'" >${CONFIG_FILE}
rm -f /tmp/pkg_path

# --------------------------------------------------
# --------------------- LDAP -----------------------
# --------------------------------------------------

# LDAP suffix.
${DIALOG} --backtitle "${DIALOG_BACKTITLE}" \
    --title "LDAP suffix" \
    --inputbox "\
Please specify your LDAP suffix.

EXAMPLE:

    +---------------------+-----------------------+
    | Your domain name    | Recommend LDAP suffix |
    +---------------------+-----------------------+
    | openbsdonly.org     | dc=openbsdonly,dc=org |
    +---------------------+-----------------------+
    | abcde.com.cn        | dc=abcde,dc=com,dc=cn |
    +---------------------+-----------------------+

" \
    20 78 "dc=openbsdonly,dc=org" \
    2>/tmp/ldap_suffix

LDAP_SUFFIX="$(cat /tmp/ldap_suffix)"
LDAP_SUFFIX_MAJOR="$(sed 's/dc=//g' /tmp/ldap_suffix | awk -F',' '{print $1}')"
LDAP_SUFFIX_MINOR="$(sed 's/dc=//g' /tmp/ldap_suffix | awk -F',' '{print $2}')"
LDAP_BINDDN="cn=${VMAIL_USER_NAME},${LDAP_SUFFIX}"
LDAP_ADMIN_DN="cn=${VMAIL_ADMIN_USER_NAME},${LDAP_SUFFIX}"
LDAP_ROOTDN="cn=Manager,${LDAP_SUFFIX}"
LDAP_BASEDN_NAME='domains'
LDAP_BASEDN="o=${LDAP_BASEDN_NAME},${LDAP_SUFFIX}"
rm -f /tmp/ldap_suffix

cat >> ${CONFIG_FILE} <<EOF
LDAP_SUFFIX="${LDAP_SUFFIX}"
LDAP_SUFFIX_MAJOR="${LDAP_SUFFIX_MAJOR}"
LDAP_SUFFIX_MINOR="${LDAP_SUFFIX_MINOR}"
LDAP_BINDDN="cn=${VMAIL_USER_NAME},${LDAP_SUFFIX}"
LDAP_ADMIN_DN="${LDAP_ADMIN_DN}"
LDAP_ROOTDN="cn=Manager,${LDAP_SUFFIX}"
LDAP_BASEDN_NAME="domains"
LDAP_BASEDN="o=${LDAP_BASEDN_NAME},${LDAP_SUFFIX}"
EOF

# LDAP rootpw.
while : ; do
    ${DIALOG} --backtitle "${DIALOG_BACKTITLE}" \
    --title "Password for LDAP rootdn: ${LDAP_ROOTDN}" \
    --inputbox "\
Please specify password for LDAP 'rootdn':

    * ${LDAP_ROOTDN}

Warnning:

    * EMPTY password is *NOT* permit.
" \
    20 78 \
    2>/tmp/ldap_rootpw

    LDAP_ROOTPW="$(cat /tmp/ldap_rootpw)"
    if [ ! -z "${LDAP_ROOTPW}" ]; then
        break
    fi
done

echo "LDAP_ROOTPW='${LDAP_ROOTPW}'" >>${CONFIG_FILE}
rm /tmp/ldap_rootpw

# VMAIL USER PASSWORD
while : ; do
    ${DIALOG} --backtitle "${DIALOG_BACKTITLE}" \
    --title "Password for LDAP bind dn and admin dn" \
    --inputbox "\
Please specify password for LDAP bind dn and admin dn:

    * bind dn:  ${LDAP_BINDDN}
    * admin dn: ${LDAP_ADMIN_DN}

Warnning:

    * EMPTY password in *NOT* permit.

" \
    20 78 \
    2>/tmp/vmail_user_passwd

    LDAP_BINDPW="$(cat /tmp/vmail_user_passwd)"
    LDAP_ADMIN_PW="${LDAP_BINDPW}"
    if [ ! -z "${LDAP_BINDPW}" ]; then
        break
    fi
done

echo "LDAP_BINDPW='${LDAP_BINDPW}'" >>${CONFIG_FILE}
echo "LDAP_ADMIN_PW='${LDAP_BINDPW}'" >>${CONFIG_FILE}
rm -f /tmp/vmail_user_passwd

# INFO
${DIALOG} --backtitle "${DIALOG_BACKTITLE}" \
    --title "LDAP bind and manage info" \
    --msgbox "\
Important Note:

    * Bind info for all LDAP operations:

        * LDAP server:  ${LDAP_SERVER_HOST}
        * LDAP port:    ${LDAP_SERVER_PORT}
        * LDAP version: ${LDAP_BIND_VERSION}
        * Bind dn:      ${LDAP_BINDDN}
        * Bind passwdw: ${LDAP_BINDPW}

    * All LDAP attributions can be managed by:

        * LDAP dn: ${LDAP_ADMIN_DN}
        * LDAP pw: ${LDAP_ADMIN_PW}

    * phpLDAPadmin: http://$(hostname)/phpldapadmin/
" \
    20 78 

# VMAIL_USER_HOME_DIR
VMAIL_USER_HOME_DIR="/home/vmail/${LDAP_BASEDN_NAME}"
${DIALOG} --backtitle "${DIALOG_BACKTITLE}" \
    --title "HOME directory of VMAIL user" \
    --inputbox "\
Please specify the HOME directory of vmail user: ${VMAIL_USER_NAME}.

EXAMPLE:

    * ${VMAIL_USER_HOME_DIR}

NOTE:

    * All mails will be stored in this HOME directory, so it may take
      large disk space.
" \
    20 78 "${VMAIL_USER_HOME_DIR}" \
    2>/tmp/vmail_user_home_dir

VMAIL_USER_HOME_DIR="$(cat /tmp/vmail_user_home_dir)"
echo "VMAIL_USER_HOME_DIR='${VMAIL_USER_HOME_DIR}'" >>${CONFIG_FILE}
rm -f /tmp/vmail_user_home_dir

# Your domain name.
${DIALOG} --backtitle "${DIALOG_BACKTITLE}" \
    --title "Your first virtual domain" \
    --inputbox "\
Please specify the name of your first virtual domain.

EXAMPLE:

    * ${LDAP_SUFFIX_MAJOR}.${LDAP_SUFFIX_MINOR}

The LDAP dn of your domain will be:

    * o=${LDAP_SUFFIX_MAJOR}.${LDAP_SUFFIX_MINOR},${LDAP_BASEDN}
" \
    20 78 "${LDAP_SUFFIX_MAJOR}.${LDAP_SUFFIX_MINOR}" \
    2>/tmp/ldap_your_domain

LDAP_YOUR_DOMAIN="$(cat /tmp/ldap_your_domain)"
echo "LDAP_YOUR_DOMAIN='${LDAP_YOUR_DOMAIN}'" >>${CONFIG_FILE}
rm -f /tmp/ldap_your_domain

#LDAP_YOUR_DOMAIN_ADMIN_NAME
LDAP_YOUR_DOMAIN_ADMIN_NAME="admin"
${DIALOG} --backtitle "${DIALOG_BACKTITLE}" \
    --title "Specify administrator' name of your virtual domain" \
    --inputbox "\
Please specify administrator' name of your virtual domain.

EXAMPLE:

    * ${LDAP_YOUR_DOMAIN_ADMIN_NAME}
    * postmaster

The full mail address and LDAP dn of the user will be:

    * ${LDAP_YOUR_DOMAIN_ADMIN_NAME}@${LDAP_YOUR_DOMAIN}
    * mail=${LDAP_YOUR_DOMAIN_ADMIN_NAME}@${LDAP_YOUR_DOMAIN}, \\
      o=${LDAP_YOUR_DOMAIN}, \\
      ${LDAP_BASEDN}
" \
    20 78 "${LDAP_YOUR_DOMAIN_ADMIN_NAME}" \
    2>/tmp/LDAP_YOUR_DOMAIN_ADMIN_NAME

LDAP_YOUR_DOMAIN_ADMIN_NAME="$(cat /tmp/LDAP_YOUR_DOMAIN_ADMIN_NAME)"
echo "LDAP_YOUR_DOMAIN_ADMIN_NAME='${LDAP_YOUR_DOMAIN_ADMIN_NAME}'" >>${CONFIG_FILE}
rm -f /tmp/LDAP_YOUR_DOMAIN_ADMIN_NAME

# LDAP_YOUR_DOMAIN_ADMIN_PASSWD
while : ; do
    ${DIALOG} --backtitle "${DIALOG_BACKTITLE}" \
    --title "Password for the administrator of your domain" \
    --inputbox "\
Please specify password for the administrator user:

    * ${LDAP_YOUR_DOMAIN_ADMIN_NAME}@${LDAP_YOUR_DOMAIN}

Warnning:

    * EMPTY password in *NOT* permit.

" \
    20 78 \
    2>/tmp/LDAP_YOUR_DOMAIN_ADMIN_PASSWD

    LDAP_YOUR_DOMAIN_ADMIN_PASSWD="$(cat /tmp/LDAP_YOUR_DOMAIN_ADMIN_PASSWD)"
    if [ ! -z "${LDAP_YOUR_DOMAIN_ADMIN_PASSWD}" ]; then
        break
    fi
done

echo "LDAP_YOUR_DOMAIN_ADMIN_PASSWD='${LDAP_YOUR_DOMAIN_ADMIN_PASSWD}'" >>${CONFIG_FILE}
rm -f /tmp/LDAP_YOUR_DOMAIN_ADMIN_PASSWD

#LDAP_EXAMPLE_USER
LDAP_EXAMPLE_USER='www'
${DIALOG} --backtitle "${DIALOG_BACKTITLE}" \
    --title "Add a example user for your virtual domain" \
    --inputbox "\
Please specify the username of your first virtual domain: ${LDAP_YOUR_DOMAIN}.

EXAMPLE:

    * ${LDAP_EXAMPLE_USER}

The full mail address of the user will be:

    * ${LDAP_EXAMPLE_USER}@${LDAP_YOUR_DOMAIN}
" \
    20 78 "${LDAP_EXAMPLE_USER}" \
    2>/tmp/LDAP_EXAMPLE_USER

LDAP_EXAMPLE_USER="$(cat /tmp/LDAP_EXAMPLE_USER)"
echo "LDAP_EXAMPLE_USER='${LDAP_EXAMPLE_USER}'" >>${CONFIG_FILE}
rm -f /tmp/LDAP_EXAMPLE_USER

# LDAP_EXAMPLE_USER_PASSWD
while : ; do
    ${DIALOG} --backtitle "${DIALOG_BACKTITLE}" \
    --title "Password for your example user" \
    --inputbox "\
Please specify password for your example user:

    * ${LDAP_EXAMPLE_USER}@${LDAP_YOUR_DOMAIN}

Warnning:

    * EMPTY password in *NOT* permit.

" \
    20 78 \
    2>/tmp/LDAP_EXAMPLE_USER_PASSWD

    LDAP_EXAMPLE_USER_PASSWD="$(cat /tmp/LDAP_EXAMPLE_USER_PASSWD)"
    if [ ! -z "${LDAP_EXAMPLE_USER_PASSWD}" ]; then
        break
    fi
done

echo "LDAP_EXAMPLE_USER_PASSWD='${LDAP_EXAMPLE_USER_PASSWD}'" >>${CONFIG_FILE}
rm -f /tmp/LDAP_EXAMPLE_USER_PASSWD

${DIALOG} --backtitle "${DIALOG_BACKTITLE}" \
    --title "Specify mail alias for 'root' user" \
    --inputbox "\
Please specify a mail alias address for 'root' user:
" \
    20 78 \
    2>/tmp/mail_alias_root

MAIL_ALIAS_ROOT=$(cat /tmp/mail_alias_root)
echo "MAIL_ALIAS_ROOT=${MAIL_ALIAS_ROOT}" >> ${CONFIG_FILE}
rm /tmp/mail_alias_root

cat <<EOF
Configuration completed.
Remember to delete file 'tools/config' after install successfully.
EOF
